var uid,user,name,pic,ws,spage, type;
document.addEventListener('plusready', function() {	
	uid = plus.storage.getItem('uid');//用户id
	user = plus.storage.getItem('user');//登录手机号
	name = plus.storage.getItem('name') != 'null'?plus.storage.getItem('name'):user;//用户名称
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	console.log(uid+" "+name); 
	//图片上传	
//	$('#uploadPhoto').on('tap', function() {
//		$('#imageUpload').click();
//	})
	//上传缩略图
	$('.litpic').on('tap', function(){
		var _this = $(this);
		type = '1';
		plus.gallery.pick( function(path){
			console.log(_this.siblings('img').length);
			if(_this.siblings('img').length > 0){
				_this.siblings('img').attr('src', path);
			}else{
			 	_this.before('<img src="'+path+'"/>');
			}
		 	appendPic(path);
		}, function(error){}, {} );
	})
	//图文详情
	$('.uploadpic').on('tap', function(){
		mui('#sheet1').popover('toggle');
	})
	//选择拍照添加
	$('.paizhao').on('tap', function(){
		type = '2';
		var cmr = plus.camera.getCamera(); 
		cmr.captureImage( function(path){
			appendPic(path);
		}, function(err){}, {index:1} );   
	})
	//选择相册添加
	$('.photo').on('tap', function(){
		type = '2';
		plus.gallery.pick( function(path){
		 	appendPic(path);
		}, function(error){}, {} );
	})
	var index = 1;
	var files = [];
	// 添加照片
	function appendPic(p) {
		showWating();
		//创建新路径 
		index = 1;
		files = [];
//		var newPath = p.replace(/\./g , new Date().getTime()+'.');
//		plus.zip.compressImage({
//				src:p,
//				dst:newPath,
//				quality:20
//			},
//			function() {//成功回调
				files.push({name:"uploadkey"+index,path:p});
				index++;
				upload();
//			},function(error) {
//				toast('压缩图片失败');
//		});
	}
	//创建对像
	function upload(){ 
		var  task = plus.uploader.createUpload(apiRoot+'?m=Home&c=Member&a=uploadOne',
		{ method:"POST"},function ( t, status ) {
			// 上传完成     
			if ( status == 200 ) { 
				var pro = t.responseText;
				console.log(pro);
				if(pro == -1){
					toast('图片上传失败');
				}else{
					if(type == '1'){
						pic = pro;
					}else{
						$('#cont').append('<img src="'+webRoot+pro+'"/>');
					}
				}
				plus.nativeUI.closeWaiting();
			} else {
				plus.nativeUI.toast( "上传失败："+status );
				plus.nativeUI.closeWaiting();
			}  
		});
		for(var i=0;i<files.length;i++){
			var f=files[i];
			task.addFile(f.path,{key:f.name});
		}
		task.start();
	}
	//写入	
	$("#submit").on('click',function(){
		var title = $(".title").val();
		var description = $("textarea[name=description]").val();
		var content = $("#cont").html();
		console.log(title+" "+description+" "+content+" "+pic)
		if(!title.trim() || !description.trim() || !content.trim() || !pic){
			toast('请完善信息！');return;
		}
		content = content.replace(webRoot, "");
		$.ajax({ 
			type:"get",
			url:apiRoot+'?m=Home&c=article&a=setNews',
			data :{
				issuedname :name,
				title :title,
				litpic :pic,
				description :description,
				body :content,
				
			},
			dataType:"json",
			success:function(data){
				if(data){
					plus.nativeUI.closeWaiting();
					toast('发布成功');
					ws.close();
				}
			},
			error:function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	});
}, false);